import { Service } from "./service";

let { protocol } = location;
export class UserService extends Service {
    url(path: string) {
        return `${protocol}//${Service.config.serviceHost}/auth/${path}`
    }
    allResources() {
        let url = this.url(`permission/resources`)
        return this.get<Resource[]>(url)
    }
    addRole(name: string, remark?: string): Promise<Role> {
        let url = this.url('role/add')
        let data = { name, remark }
        return this.postByJson<Role>(url, data)
    }
    roles() {
        let url = this.url('role/list')
        return this.getByJson<Role[]>(url)
    }
    updateRole(role: Role) {
        console.assert(role != null)
        console.assert(role.id != null);
        let url = this.url('role/update')
        return this.postByJson<Role>(url, role)
    }
    removeRole(id: string) {
        let url = this.url('role/remove')
        return this.postByJson<Role>(url, { id })
    }
    getRole(id: string) {
        let url = this.url('role/get')
        return this.getByJson<Role>(url, { id })
    }
    roleResources(roleId: string) {
        let url = this.url('role/resourceIds')
        return this.getByJson<RoleResource[]>(url, { roleId })
    }
    setResources(roleId: string, resourceIds: string[]) {
        let url = this.url('role/setResources')
        return this.postByJson(url, { roleId, resourceIds })
    }
    applicationUsers() {
        let url = this.url('application/users/')
        return this.getByJson<User[]>(url, {})
    }
    applicationAddUser(mobile: string, roleIds: string[]): any {
        let url = this.url('application/addUser')
        return this.postByJson<{ user_id: string }>(url, { mobile, roleIds })
    }
    applicationUpdateUser(userId: string, roleIds: string[]) {
        let url = this.url('application/updateUser')
        return this.postByJson(url, { userId, roleIds })
    }
}